package cn.wolfcode.controller;

import cn.wolfcode.domain.Region;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IRegionService;
import cn.wolfcode.util.JsonResult;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @Description: 区域控制器类
 * @Author: 伍家钰
 * @Date: 2020/8/10 10:40
 * @Version: 1.0
 */
@Controller
@RequestMapping("region")
public class RegionController {
    @Resource
    private IRegionService regionService;
    @Resource
    private IDestinationService destinationService;

    /**
     * 分页查询
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        Page<Region> page = regionService.query(qo);
        model.addAttribute("page", page);
        return "region/list";
    }

    /**
     * 查询目的地数据
     *
     * @return
     */
    @RequestMapping("listDests")
    @ResponseBody
    public Object listDests() {
        return destinationService.list();
    }

    /**
     * 保存或修改
     *
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region) {
        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }


    /**
     * 查询指定区域关联的目的地,要查什么数据就用什么service
     *
     * @param rid
     * @return
     */
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid) {
        return destinationService.queryDestByRefIds(rid);
    }

    /**
     * 修改热键状态
     *
     * @param hot:
     * @param id:
     * @return :
     */
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot, String id) {
        regionService.changeHotValue(hot, id);
        return JsonResult.success();
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        regionService.delete(id);
        return JsonResult.success();
    }

}
